package com.cad3d.filemanager.action;

import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.cad3d.filemanager.dao.Filemanagement;
import com.cad3d.filemanager.dao.User;
import com.cad3d.filemanager.dbutils.FileManagementTableTool;
import com.cad3d.filemanager.utils.StringUtils;
import org.apache.log4j.Logger;

/**
 * @author bau
 * 
 */
public class FileManagerAction {

	User user = (User) ServletActionContext.getRequest().getSession().getAttribute("loggedInUser");

	private Logger logger = Logger.getLogger(FileManagerAction.class);
	
	private List<Filemanagement> listFiles = new ArrayList<Filemanagement>();
	private Integer rows = 0;
	private Integer page = 0;
	private String sord = "asc";
	private String sidx;
	private String searchField;
	private String searchString;
	private String searchOper;
	private Integer total = 0;
	private Integer records = 0;
	private List<Filemanagement> listAllFiles = new ArrayList<Filemanagement>();

	public String execute() {
		
		String query = (String) ServletActionContext.getRequest().getSession()
				.getAttribute("query");
		if (!StringUtils.isNullOrEmpty(query)) {
			ServletActionContext.getRequest().getSession().removeAttribute("query");
			listAllFiles = FileManagementTableTool.getInstance()
					.findFileByFilename(query);
		} else {
			if (searchField != null) {
				if (searchField.equals("fileName")) {
					if (searchOper.equalsIgnoreCase("eq")) {
						listAllFiles = FileManagementTableTool.getInstance()
								.findFileByFilename(searchString);
					}
				} else if (searchField.equals("fileType")) {
					if (searchOper.equalsIgnoreCase("eq")) {
						listAllFiles = FileManagementTableTool.getInstance()
								.findFileByExtension(searchString);
					}
				}
			} else {
				if(user.isIsAdmin()) {	
					listAllFiles = FileManagementTableTool.getInstance()
						.getAllFiles();
				}else {		
					listAllFiles = FileManagementTableTool.getInstance()
							.getAllFilesPublic(user);
				}
			}
		}

		int to = (rows * page);
		int from = to - rows;

		records = listAllFiles.size();
		if (to > records) {
			listFiles = listAllFiles.subList(from, records);
		} else {
			listFiles = listAllFiles.subList(from, to);
		}
		total = (int) Math.ceil((double) records / (double) rows);

		return "success";
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public String getSearchField() {
		return searchField;
	}

	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	public String getSearchOper() {
		return searchOper;
	}

	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getRecords() {
		return records;
	}

	public void setRecords(Integer records) {
		this.records = records;
	}

	public List<Filemanagement> getListFiles() {
		return listFiles;
	}

	public void setListFiles(List<Filemanagement> listFiles) {
		this.listFiles = listFiles;
	}

}
